home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Screenblankers / GBlanker / GSource / Blankers / Waves / blank.c next >
C/C++ Source or Header  |  1996-09-26  |  9KB  |  451 lines

  1. /*
  2. **            Waves Blanker
  3. **
  4. **        Copyright © 1995 by Marzio De Biasi
  5. **
  6. **
  7. **    Source:        blank.c
  8. **    Language:    ANSI C
  9. **    Version:    1.0
  10. **    Description:    a module for GarshneBlanker v38.8 modular screen
  11. **            blanker by Micheal D. Bayne, see Waves.doc for
  12. **            further details.
  13. **    Last update:    21 Feb 95        
  14. **    Author:        De Biasi Marzio
  15. **            Address: via Borgo Simoi, 34
  16. **                 31029 Vittorio Veneto (TV)
  17. **                 Italy
  18. **            E-Mail:  debiasi@dimi.uniud.it
  19. **
  20. **    History:    
  21. **
  22. **    ->
  23. **
  24. */
  25.  
  26. #include "/includes.h"
  27.  
  28. #include <graphics/rastport.h>
  29. #include <graphics/gfx.h>
  30. #include <clib/exec_protos.h>
  31. #include <math.h>
  32. #include <time.h>
  33. #include <stdio.h>
  34.  
  35. #define PREF_SPOTS     0
  36. #define PREF_WLEN    2
  37. #define PREF_SIZ    4
  38. #define PREF_DELAY    6
  39. #define MODE        8
  40.  
  41. LONG Hei, Wid;
  42. Triplet *ColorTable = 0L;
  43. WORD Cl;
  44.  
  45. struct Screen     *pubWav_sc;
  46. struct RastPort *pubWav_rp;
  47. struct RastPort *pubWav_trp;
  48. struct BitMap    *pubWav_tbm;
  49.  
  50. #define MAX_SPOTS    5
  51.  
  52. struct Spot {
  53.     WORD    x;
  54.     WORD    y;
  55. };
  56. typedef struct Spot Spot_t;
  57.  
  58. #define PIx2        6.2831853
  59. #define MAX_WLEN    120
  60. #define MAX_WABS    100
  61.  
  62. WORD    pubWav_tdpth;
  63. WORD    pubWav_tw;
  64. LONG    pubWav_siz = 4;
  65. LONG    pubWav_wlen = 10;
  66. LONG    pubWav_spots;
  67. Spot_t  pubWav_s[MAX_SPOTS];
  68. WORD    pubWav_sin[MAX_WLEN];
  69.  
  70. ULONG    pubWav_delay = 0;
  71.  
  72. #include "Waves_rev.h"
  73. STATIC const UBYTE VersTag[] = VERSTAG;
  74.  
  75.  
  76.  
  77.  
  78. /* ------------------------------------------------------------------- */
  79. /* ---- Defaults() --------------------------------------------------- */
  80. /* ------------------------------------------------------------------- */
  81.  
  82. VOID Defaults( PrefObject *Prefs )
  83. {
  84.     Prefs[PREF_SPOTS].po_Level = 3;
  85.     Prefs[PREF_WLEN].po_Level = 50;
  86.     Prefs[PREF_SIZ].po_Level = 4;
  87.     Prefs[PREF_DELAY].po_Level = 15;
  88.     Prefs[MODE].po_ModeID = getTopScreenMode();
  89.     Prefs[MODE].po_Depth = 5;
  90. }
  91.  
  92.  
  93.  
  94.  
  95. /* ------------------------------------------------------------------- */
  96. /* ---- BuildTmpRaster() --------------------------------------------- */
  97. /* ------------------------------------------------------------------- */
  98.  
  99. BOOL    BuildTmpRaster( void ) {
  100.  
  101.     WORD    i;
  102.  
  103.     if (pubWav_trp = (struct RastPort *)malloc(sizeof(struct RastPort))) {
  104.  
  105.     InitRastPort(pubWav_trp);
  106.  
  107.     pubWav_trp->Layer = NULL;
  108.  
  109.     if (pubWav_tbm = (struct BitMap *)malloc(sizeof(struct BitMap))) {
  110.  
  111.         InitBitMap(pubWav_tbm, pubWav_tdpth, pubWav_tw, 1);
  112.  
  113.         /* alloc planes */
  114.         for (i = 0; i < pubWav_tdpth; i++)
  115.         if (! (pubWav_tbm->Planes[i] = AllocRaster(pubWav_tw,4))) break; 
  116.  
  117.         if (i != pubWav_tdpth)
  118.         return(FALSE);
  119.         else  {
  120.         pubWav_trp->BitMap = pubWav_tbm;
  121.         return(TRUE);
  122.         }
  123.         
  124.     }
  125.     else
  126.         return(FALSE);
  127.  
  128.     }
  129.     else
  130.     return(FALSE);
  131. }
  132.  
  133.  
  134.  
  135.  
  136. /* ------------------------------------------------------------------- */
  137. /* ---- FreeTmpRaster() ---------------------------------------------- */
  138. /* ------------------------------------------------------------------- */
  139.  
  140. VOID    FreeTmpRaster( void ) {
  141.  
  142.     WORD    i;
  143.  
  144.     if (pubWav_tbm) {
  145.  
  146.     for (i = 0; i < pubWav_tdpth; i++)
  147.         if (pubWav_tbm->Planes[i]) FreeRaster(pubWav_tbm->Planes[i],pubWav_tw,4); 
  148.  
  149.     free(pubWav_tbm);
  150.     }
  151.  
  152.     if (pubWav_trp) free(pubWav_tbm);
  153. }
  154.  
  155.  
  156.  
  157.  
  158. /* ------------------------------------------------------------------- */
  159. /* ---- SetUpField() ------------------------------------------------- */
  160. /* ------------------------------------------------------------------- */
  161.  
  162. LONG    SetUpField( void ) {
  163. /*
  164. **  Make waves.
  165. */
  166.     register    UBYTE    *line;
  167.     register     WORD x;
  168.     register     WORD y;
  169.     register     WORD i;
  170.  
  171.     LONG    RetVal;
  172.     WORD    c, oldc = -1;
  173.     float    t, dum;
  174.     LONG    t1, t2, dist, wsum, max_sum, wp;
  175.  
  176.  
  177.     if ((line = (UBYTE *)malloc(pubWav_tw)) == NULL) return(FAILED);
  178.     
  179.     t = PIx2 / pubWav_wlen;
  180.  
  181.     dum = 0.;
  182.     i = 0;
  183.     while (i < pubWav_wlen) {
  184.     pubWav_sin[i] = sin(dum) * MAX_WABS + 0.5;
  185.     dum += t;
  186.     i++;
  187.     } 
  188.  
  189.     for (i = 0; i < pubWav_spots; i++) {
  190.     pubWav_s[i].x = lrand48() % Wid;
  191.     pubWav_s[i].y = lrand48() % Hei;
  192.     }
  193.  
  194.     wp = MAX_WABS * pubWav_spots;
  195.     max_sum = wp * 2;
  196.  
  197.     for (y = 0; y < Hei; y++) {
  198.     for (x =0; x < Wid; x++) {
  199.         wsum = 0;
  200.         for (i = 0; i < pubWav_spots; i++) {
  201.  
  202.         t1 = (pubWav_s[i].x - x);
  203.         t2 = (pubWav_s[i].y - y);
  204.  
  205.         dist = t1*t1 + t2*t2;
  206.  
  207.         dist = sqrt((float)dist) + 0.5;
  208. /*
  209. **        t1 = 0;
  210. **        while ((t1 * t1) < dist) t1++;
  211. **        dist = t1 - 1;
  212. */
  213.  
  214.         wsum += pubWav_sin[dist % pubWav_wlen];
  215.         }
  216.         wsum += wp;
  217.  
  218. /*        c = ((float)wsum * (Cl-1)) / (float)max_sum + 0.5;  */
  219.  
  220.         c = (wsum * (Cl-2)) / max_sum + 1;
  221.  
  222. /* old engine 
  223. **        if (pubWav_siz == 1) {
  224. **        line[x] = c;
  225. **        }
  226. **        else {
  227. **        if (c != oldc) { oldc = c; SetAPen(pubWav_rp,c); }
  228. **        t1 = x * pubWav_siz;
  229. **            t2 = y * pubWav_siz;
  230. **        RectFill(pubWav_rp,t1,t2,t1+pubWav_siz-1,t2+pubWav_siz-1);
  231. **        }
  232. **
  233. **        if ((RetVal = ContinueBlanking()) != OK) goto lwav_dskip;
  234. **    }
  235. **
  236. **    if (pubWav_siz == 1)
  237. **        WritePixelLine8(pubWav_rp,0,y,pubWav_tw,line,pubWav_trp); 
  238. */
  239.  
  240.  
  241. /* new engine */
  242.  
  243.         t1 = x * pubWav_siz;
  244.         for (i = 0; i < pubWav_siz; i++) line[t1 + i] = c;
  245.  
  246.         if ((RetVal = ContinueBlanking()) != OK) goto lwav_dskip;
  247.     }
  248.  
  249.     t2 = y * pubWav_siz;
  250.     WritePixelLine8(pubWav_rp,0,t2,pubWav_tw,line,pubWav_trp); 
  251.     for (i = 1; i < pubWav_siz; i++) 
  252.         BltBitMap(pubWav_rp->BitMap,0,t2,pubWav_rp->BitMap,0,t2+i, \
  253.         pubWav_tw,1,0x0C0,0xFF,NULL);
  254.      }
  255.  
  256. lwav_dskip:
  257.     if (line) free(line);
  258.  
  259.     return(RetVal);
  260. }
  261.  
  262.  
  263.  
  264.  
  265. /* ------------------------------------------------------------------- */
  266. /* ---- WavesDriver() ------------------------------------------------ */
  267. /* ------------------------------------------------------------------- */
  268.  
  269. LONG    WavesDriver( void ) {
  270.  
  271.     LONG    RetVal;
  272.     ULONG    scr2front = 0, delay_count;
  273.     WORD    i;
  274.     ULONG    *ctab, tr, tg, tb, c;
  275.     UBYTE    rf, gf, bf;
  276.     UWORD    *OldCTab;
  277.     float    dum;
  278.  
  279.     if (!BuildTmpRaster()) { RetVal = FAILED; goto lwav_err;  }
  280.  
  281.  
  282. /*    ctab = NULL; */
  283.     ctab = GetColorTable(pubWav_sc); 
  284.  
  285.     rf = lrand48() & 1;
  286.     gf = lrand48() & 1;
  287.     bf = lrand48() & 1;
  288.     if ((rf + gf +bf) == 0) bf = 1;
  289.  
  290.  
  291.     if (ctab) {
  292.  
  293.         ctab[1] = 0;
  294.         ctab[2] = 0;
  295.         ctab[3] = 0;
  296.  
  297.     dum = 255.0/(Cl-2);
  298.  
  299.         for (i = 1; i < (Cl/2 + 1); i++) {
  300.  
  301.         c = (i -1) * 2.0 * dum;
  302.         if (c > 0xFF) c = 0xFF;
  303.         c &= 0xFF;
  304.  
  305.         c = c | (c << 8) | (c << 16) | (c<<24);
  306.  
  307.         ctab[i*3+1] = c * rf;             
  308.         ctab[i*3+2] = c * gf;             
  309.         ctab[i*3+3] = c * bf;             
  310.  
  311.         ctab[(Cl-i)*3+1] = c * rf;             
  312.         ctab[(Cl-i)*3+2] = c * gf;             
  313.         ctab[(Cl-i)*3+3] = c * bf;             
  314.     }
  315.         LoadRGB32(&(pubWav_sc->ViewPort), ctab);
  316.     }
  317.     else {
  318.  
  319.     OldCTab = (pubWav_sc->ViewPort.ColorMap)->ColorTable;
  320.     OldCTab[0] = 0;
  321.  
  322.     dum = 15.0/(Cl-2);
  323.  
  324.     if (dum == 0) dum = 1;
  325.  
  326.         for (i = 1; i < (Cl/2 + 1); i++) {
  327.  
  328.         c = (i -1) * 2 * dum;
  329.         if (c > 0xF) c = 0xF;
  330.         c &= 0xF;
  331.  
  332.         if (gf)  c |= (c << 4);
  333.         if (bf)  c |= (c << 8);
  334.         if (!rf) c &= 0xFF0;
  335.         
  336.         OldCTab[i] = c;             
  337.         OldCTab[Cl-i] = c;             
  338.     }
  339.         LoadRGB4(&(pubWav_sc->ViewPort), OldCTab, Cl);
  340.     }
  341.  
  342.     RetVal = SetUpField(); 
  343.  
  344.     delay_count = 0;
  345.     while (RetVal == OK) {
  346.  
  347.     if ((scr2front++ % 100) == 0) ScreenToFront(pubWav_sc); 
  348.  
  349.     WaitTOF(); 
  350.  
  351.  
  352.     if (!pubWav_delay || ((delay_count++ % pubWav_delay) == 0)) {
  353.  
  354.         if (ctab != NULL) {
  355.  
  356.             tr = ctab[4];
  357.             tg = ctab[5];
  358.             tb = ctab[6];
  359.             for (i = 1; i < (Cl-1); i++) {
  360.             ctab[i*3 + 1] = ctab[(i+1)*3 +1];
  361.             ctab[i*3 + 2] = ctab[(i+1)*3 +2];
  362.             ctab[i*3 + 3] = ctab[(i+1)*3 +3];
  363.             }
  364.             ctab[Cl*3 - 2] = tr;
  365.             ctab[Cl*3 - 1] = tg;
  366.             ctab[Cl*3 ] = tb;
  367.                 LoadRGB32(&(pubWav_sc->ViewPort), ctab);
  368.         }
  369.         else {
  370.         tr = OldCTab[1];
  371.             for (i = 1; i < (Cl-1); i++) OldCTab[i] = OldCTab[i+1];
  372.             OldCTab[Cl - 1] = tr;
  373.             LoadRGB4(&(pubWav_sc->ViewPort), OldCTab, Cl);
  374.         }
  375.     }    
  376.         if ((RetVal = ContinueBlanking()) != OK) break;
  377.     }
  378.  
  379.     if (ctab) FreeVec(ctab);
  380.  
  381. lwav_err:
  382.     FreeTmpRaster();
  383.  
  384.     return(RetVal);
  385. }
  386.  
  387.  
  388.  
  389.  
  390. /* ------------------------------------------------------------------- */
  391. /* ---- Blank() ------------------------------------------------------ */
  392. /* ------------------------------------------------------------------- */
  393.  
  394. LONG Blank( PrefObject *Prefs )
  395. {
  396. /*
  397. **  Opens screen, initializes color table, preference values and random
  398. **  numbers generator seed, then call <AntDriver()>.
  399. */
  400.     struct Window *Wnd;
  401.     LONG  RetVal;
  402.  
  403.     srand48(time(0L));
  404.  
  405.     if (Prefs[MODE].po_Depth < 3) Prefs[MODE].po_Depth = 3;
  406.  
  407.     pubWav_sc = OpenScreenTags( NULL, SA_Depth, Prefs[MODE].po_Depth, SA_Quiet, TRUE,
  408.                          SA_DisplayID, Prefs[MODE].po_ModeID, SA_Behind, TRUE,
  409.                          SA_Overscan, OSCAN_STANDARD, TAG_DONE );
  410.  
  411.     if( pubWav_sc )
  412.     {
  413.  
  414.         ScreenToFront(pubWav_sc);
  415.  
  416.         pubWav_rp = &pubWav_sc->RastPort;
  417.  
  418.         Cl = 1L << Prefs[MODE].po_Depth;
  419.  
  420.         pubWav_tdpth = Prefs[MODE].po_Depth;
  421.         pubWav_tw = pubWav_sc->Width;
  422.  
  423.         pubWav_spots = Prefs[PREF_SPOTS].po_Level;
  424.         pubWav_delay = Prefs[PREF_DELAY].po_Level & 0xFFFF;
  425.         pubWav_wlen = Prefs[PREF_WLEN].po_Level;
  426.         pubWav_siz = Prefs[PREF_SIZ].po_Level;
  427.  
  428.  
  429.         Wid = pubWav_sc->Width / pubWav_siz;
  430.         Hei = pubWav_sc->Height / pubWav_siz;
  431.  
  432. /*            ColorTable = RainbowPalette( pubWav_sc, 0L, 1L, 0L );  */
  433.  
  434.         Wnd = BlankMousePointer( pubWav_sc );
  435.         
  436.     
  437.  
  438.         RetVal = WavesDriver();
  439.  
  440.         UnblankMousePointer( Wnd );
  441.  
  442. /*        RainbowPalette( 0L, ColorTable, 1L, 0L );    */
  443.  
  444.         CloseScreen( pubWav_sc );
  445.     }
  446.     else
  447.         RetVal = FAILED;
  448.     
  449.     return(RetVal);
  450. }
  451.